package shop.yaojianfeng.springlearning.section6.entity;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.ejb.Init;
import javax.sql.DataSource;
import java.util.ResourceBundle;

/**
 * 使用注解方式配置数据源
 * @author yaojianfeng
 */
@Configuration
public class JdbcConfig {

    @Bean
    public DataSource getDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        druidDataSource.setDriverClassName(bundle.getString("driverName"));
        druidDataSource.setUrl(bundle.getString("url"));
        druidDataSource.setUsername(bundle.getString("username"));
        druidDataSource.setPassword(bundle.getString("password"));
        return druidDataSource;
    }

    @Bean
    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate(){
        System.out.println("使用java配置加载NamedParameterJdbcTemplate模板");
        return new NamedParameterJdbcTemplate(getDataSource());
    }
}
